<template>
    <!--时间线-->
    <div class="container">
        <div class="time-line-wrap">
            <div
                v-for="(item, index) in timeLineList"
                :key="index"
                class="line-font-wrap-com"
            >
                <div class="line-wrap">
                    <div class="item"></div>
                    <el-tooltip effect="light" placement="top">
                        <div slot="content">
                            {{ timeLineList[index].time }}
                        </div>
                        <div
                            @click="changeActive(index)"
                            :class="{
                                active: index === timeIndex,
                                blod: timeLineList[index].lineType === 'blod',
                                thin: timeLineList[index].lineType === 'thin',
                            }"
                        ></div>
                    </el-tooltip>
                    <div class="item"></div>
                </div>

                <div
                    class="item-bottom"
                    :style="getFontStyle"
                    v-if="timeLineList[index].lineType === 'blod'"
                >
                    <span>{{
                        timeLineList[index].time.split(' ')[1].split(':')[0]
                    }}</span>
                </div>
            </div>
        </div>
        <div class="btns-wrap">
            <el-button
                type="primary"
                :icon="isPlay ? 'el-icon-video-play' : 'el-icon-video-pause'"
                circle
                style="font-size: 20px"
                @click="toggleOption()"
            ></el-button>
            <el-button
                type="primary"
                icon="el-icon-refresh"
                style="font-size: 20px"
                circle
                @click="reset()"
            ></el-button>
        </div>
    </div>
</template>

<script>
export default {
    name: 'timeLine',
    props: {
        obj: { type: Object },
        timeLineList: { type: Array },
        curTimeIndex: { type: Number }
    },
    data() {
        return {
            // timeIndex: this.timeLineList.length - 1, //默认当前选择的为最近的时间点
            timeIndex: 0, //默认当前选择的时间点
            point: 0, //时间轴只展示6个,初始为最后6个
            pointEnd: 144,
            rightButtonActive: false,
            leftButtonActive: true,
            leftButtonDisabled: false,
            rightButtonDisabled: true,
            currentActiveObj: {},
            isPlay: true,
            timer: null, //定时器
            stepLength: 1, //时间刻度跳动的跨度
            timerRate: 1 //定时器刷新频率
        };
    },
    watch: {
        obj: {
            handler() {
                console.log('this.obj2', this.obj);
                if (this.obj) {
                    if (this.obj.attribute.style.showHoursNum) {
                        let showHoursNum = this.obj.attribute.style
                            .showHoursNum;

                        //以时间轴起始点为准，向后扩展showHoursNum个刻度
                        this.point = 0;
                        this.pointEnd = showHoursNum ? showHoursNum * 6 : 144; //this.timeLineList.length
                    }
                    if (
                        this.obj.attribute.style.timerRate ||
                        this.obj.attribute.style.stepLength
                    ) {
                        this.timerRate = this.obj.attribute.style.timerRate;
                        this.stepLength = this.obj.attribute.style.stepLength;
                        if (!this.isPlay) {
                            this.isPlay = true;
                            this.toggleOption();
                        }
                    }
                } else {
                    this.pointEnd = 24 * 6;
                }
            },
            immediate: true,
            deep: true
        },
        curTimeIndex: {
            handler() {
                this.timeIndex = this.curTimeIndex;
                this.currentActiveObj = this.timeLineList[this.timeIndex];
                console.log('this.curTimeIndex', this.curTimeIndex);
            },
            immediate: true
        },
        currentActiveObj: {
            handler() {
                console.log('this.currentActiveObj', this.currentActiveObj);
            },
            immediate: true
        }
    },
    computed: {
        //定义字体样式
        getFontStyle() {
            return {
                color: this.obj ? this.obj.attribute.style.color : 'white',
                fontSize: this.obj
                    ? this.obj.attribute.style.fontSize + 'px'
                    : '16px'
                //   backgroundColor: this.obj.attribute.style.backgroundColor,
            };
        }
    },
    destroyed: function() {
        // 每次离开当前界面时，清除定时器
        clearInterval(this.timer);
        this.timer = null;
    },
    methods: {
        changeActive(index) {
            this.timeIndex = index;
            this.currentActiveObj = this.timeLineList[this.timeIndex];
            console.log('悬浮选择的时间点：', this.timeIndex);
        },
        toggleOption() {
            if (this.timer) {
                clearInterval(this.timer);
            }
            if (this.isPlay) {
                let that = this;
                that.timer = setInterval(() => {
                    that.timeIndex = this.timeIndex + this.stepLength;
                    that.currentActiveObj = that.timeLineList[that.timeIndex];
                    if (that.timeIndex === that.pointEnd) {
                        //如果移到最后一个时间点，回到原点
                        that.timeIndex = 0;
                    }
                }, this.timerRate * 1000); //由于公共组件EncInputNumber设置最大number为100，所以，此处设置
            }
            this.isPlay = !this.isPlay;
        },
        //向右移动刻度
        moveRight() {
            this.timeIndex += 1;
            this.currentActiveObj = this.timeLineList[this.timeIndex];
            if (this.timeIndex === this.pointEnd) {
                //如果移到最后一个时间点，回到原点
                this.timeIndex = 0;
            }
        },
        //重置
        reset() {
            clearInterval(this.timer);
            this.timer = null;
            this.isPlay = true;
            this.timeIndex = this.curTimeIndex;
            this.currentActiveObj = this.timeLineList[this.timeIndex];
        }
    }
};
</script>

<style lang="scss" scoped>
.container {
    display: flex;
    width: 100%;
    .time-line-wrap {
        display: flex;
        flex: 1;
        .line-font-wrap-com {
            flex: 1;
            display: flex;
            flex-direction: column;
            .line-wrap {
                height: 50px;
                display: flex;
                align-items: center;
                /* flex: 1; */
                .item {
                    flex: 1;
                    border-bottom: 1px solid #dcdfe6;
                    box-sizing: border-box;
                }
            }
            .item-bottom {
                flex: 1;
                text-align: center;
                height: 15px;
                margin-top: 7px;
            }
        }
    }
    .btns-wrap {
        flex-basis: 105px;
        display: flex;
        align-items: center;
        margin-left: 12px;
    }
}

.blod {
    border: 2px solid #dcdfe6;
    border-radius: 20px;
    width: 8px;
    height: 35px;
    background: white;
    margin: 2px 0px;
    box-sizing: border-box;
    cursor: pointer;
}
.thin {
    border: 1px solid #dcdfe6;
    border-radius: 10px;
    width: 4px;
    height: 20px;
    background: white;
    margin: 2px 0px;
    box-sizing: border-box;
    cursor: pointer;
}

.move-button {
    border: 2px solid #dcdfe6;
    width: 40px;
    height: 19px;
    border-radius: 19px;
    background: white;
    text-align: center;
    box-sizing: border-box;
}
.active {
    background-color: #fff !important;
    border: 3px solid #67c23a;
}
</style>